package com.powermanagertest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener,
        OnCheckedChangeListener {

    private Button mButton, mNetWorkButton;
    private RadioGroup mrRadioGroup = null;
    private boolean isStart = false;
    private boolean isNetWorkStart = false;
    private int checked = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findView();
        setListener();
    }

    @Override
    protected void onDestroy() {

        Toast.makeText(this, "Stop", Toast.LENGTH_SHORT).show();
        stopService(new Intent(this, Myservice.class));
        isStart = false;
        super.onDestroy();
    }

    private void findView() {
        mNetWorkButton = (Button) findViewById(R.id.wifiButton);
        mButton = (Button) findViewById(R.id.button1);
        mrRadioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
    }

    private void setListener() {
        mNetWorkButton.setOnClickListener(this);
        mButton.setOnClickListener(this);
        mrRadioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            if (isStart) {
                Toast.makeText(this, "Stop", Toast.LENGTH_SHORT).show();
                stopService(new Intent(this, Myservice.class));
                isStart = false;
                mButton.setText("START");
            } else {
                Toast.makeText(this, "Start", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this, Myservice.class);
                intent.putExtra(Myservice.TIME_KEY, checked);
                startService(intent);
                isStart = true;
                mButton.setText("STOP");
            }
            break;
        case R.id.wifiButton:
            if (isStart) {
                stopService(new Intent(this, NetWorkService.class));
                isNetWorkStart = false;
            } else {
                Intent intent = new Intent(this, NetWorkService.class);
                intent.putExtra(NetWorkService.KEY, NetWorkService.DOWN_LOAD);
                startService(intent);
                isNetWorkStart = true;
            }

            break;

        default:
            break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        switch (checkedId) {
        case R.id.radio0:
            checked = 0;
            break;
        case R.id.radio1:
            checked = 1;
            break;
        case R.id.radio2:
            checked = 2;
            break;
        default:
            break;
        }
    }
}
